<template>
  <div v-loading="loading" element-loading-text="模型生成中...">
    <div style="width: 100%; border: 1px solid white;">
      <el-form  ref="form" label-width="90px">

        <div style="padding-left: 35px;font-size: 14px; color: #606266; font-weight: 500">上翼面</div>
        <el-form-item label="前缘半径">
          <el-input size="small" v-model.number="re" type="number" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">mm</i>
          </el-input>
        </el-form-item>
        <el-form-item label="最大厚度位置">
          <el-input size="small" style="width: 46%" v-model.number="xt" type="number" placeholder="请输入内容"></el-input>
          <el-input size="small"  style="width: 46%; margin-left: 4%"  v-model.number="yt" type="number" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="厚度曲率">
          <el-input size="small"  type="number" v-model.number="kt" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">mm</i>
          </el-input>
        </el-form-item>
        <el-form-item label="后楔角">
          <el-input size="small"  type="number" v-model.number="be" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">°</i>
          </el-input>
        </el-form-item>

        <div style="padding-left: 35px;font-size: 14px; color: #606266; font-weight: 500">下翼面</div>
        <el-form-item label="前缘方向">
          <el-input size="small" v-model.number="ye" type="number" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">mm</i>
          </el-input>
        </el-form-item>
        <el-form-item label="最大外倾角位置">
          <el-input size="small" style="width: 46%" v-model.number="xc" type="number" placeholder="请输入内容"></el-input>
          <el-input size="small"  style="width: 46%; margin-left: 4%"  v-model.number="yc" type="number" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="外倾角斜率">
          <el-input size="small"  type="number" v-model.number="kc" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">mm</i>
          </el-input>
        </el-form-item>
        <el-form-item label="后倾角">
          <el-input size="small"  type="number" v-model.number="at1" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">°</i>
          </el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center; margin-top: 10px">
        <el-button type="primary" size="small" @click="generatFunction">确定</el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </div>
    </div>

  </div>

</template>

<script>
  import axios from 'axios'
  import {
    Axis,
    CrownCADPlugin,
    Direction,
    PluginFunctionType,
    Point,
    PluginEventType, PluginCADToolFunctionType
  } from "../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
  import {request} from "../../../util/Request";
  export default {
    name: "Airfoil",
    data() {
      return {
        loading: false,
        minHeight: 378,
        re:-0.009,
        xt:0.355,
        yt:0.099,
        kt:-0.824,
        be: 16,
        ye:63.3,
        xc:0.45,
        yc:0.09,
        kc: 1,
        at1: 50,
        at: 0
      }
    },
    async mounted() {
      //创建插件对象
      this.crownCADPlugin = new CrownCADPlugin();
      //设置CrownCAD平台地址
      this.crownCADPlugin.setParentOrigin(window.url);
      //.设置与CrownCAD平台尝试建立连接的次数
      this.crownCADPlugin.setMaxAttempt(5);
      //设置插件在CrownCAD平台显示的尺寸
      this.crownCADPlugin.setLayout(320, 320, this.minHeight);
      // 发送与CrownCAD平台建立连接的请求
      await this.crownCADPlugin.connect()
      await this.crownCADPlugin.request.execFunction(PluginFunctionType.LAYOUT, { minWidth: 320, width: 320, maxHeight: this.minHeight})
      this.crownCADPlugin.command.clearCommand();
    },
    methods: {
      //关闭对话框
      closeDialog() {
        this.crownCADPlugin.request.execFunction(PluginCADToolFunctionType.CLOSEACTIVECMD, null);
      },
      async generatFunction() {
        // 根据公式求上翼面函数的各系数
        this.at= -1* this.at1
        let func1= 27*this.kt*this.kt/ 4
        let func2=  (27*this.kt*this.kt*this.xt)*-1
        let func3= 9*this.kt*this.yt+ 81*this.kt*this.kt*this.xt*this.xt*0.5
        let func4= this.re*2 - 18*this.kt*this.xt*this.yt- 27*this.kt*this.kt*this.xt*this.xt*this.xt
        let func5= 3* this.yt*this.yt+ 9*this.kt*this.xt*this.xt*this.yt+ 27*this.kt*this.kt*this.xt*this.xt*this.xt*this.xt/ 4
        // 对上翼面函数求实根
        let roots= await this.solveQuartic([parseFloat(func1), parseFloat(func2), parseFloat(func3), parseFloat(func4), parseFloat(func5)])
        let max= this.xt- Math.sqrt(-2*this.yt/(3*this.kt))> 0? this.xt- Math.sqrt(-2*this.yt/(3*this.kt)) :0
        // 根据限制条件找出符合条件的根
        let rt= []
        for (let i = 0; i < roots.length; i++) {
          if(roots[i]> max && roots[i]< this.xt) {
            rt.push(roots[i])
          }
        }

        // 求下翼面的函数，计算rc
        let rc= (16+ 3* this.kc*(1/Math.tan(this.ye/180* Math.PI)+ 1/Math.tan(this.at/180* Math.PI)))/(3*this.kc*(1/Math.tan(this.ye/180* Math.PI)+ 1/Math.tan(this.at/180* Math.PI)))
        let rc1= 4*Math.sqrt(16+ 6* this.kc*(1/Math.tan(this.ye/180* Math.PI)+ 1/Math.tan(this.at/180* Math.PI))*(1 - this.yc*(1/Math.tan(this.ye/180* Math.PI)+ 1/Math.tan(this.at/180* Math.PI))))
        // 输出解
        rc= rc+ rc1

        // 计算上翼面的7个点并存储到pickPoints数组中
        let point1= new Point(0,0)
        let point2= new Point(0,3* this.kt* (this.xt- rt)*(this.xt- rt)*0.5+ this.yt)
        let point3= new Point(rt[0],this.yt)
        let point4= new Point(this.xt,this.yt)
        let point5= new Point(2* this.xt- rt[0],this.yt)
        let point6= new Point(1-(3* this.kt*(this.xt -rt[0])*(this.xt -rt[0])*0.5+ this.yt)*1/Math.tan(this.be/180* Math.PI),3* this.kt*(this.xt -rt[0])*(this.xt -rt[0])*0.5+ this.yt)
        let point7= new Point(1,0)

        let pickPoints= []
        pickPoints.push(point1)
        pickPoints.push(point2)
        pickPoints.push(point3)
        pickPoints.push(point4)
        pickPoints.push(point5)
        pickPoints.push(point6)
        pickPoints.push(point7)

        // 计算下翼面的7个点并存储到pickPoints1数组中
        let pickPoints1= []
        let point11= new Point(0,0)
        let point12= new Point(rc* 1/Math.tan(this.ye/180* Math.PI),rc)
        let point13= new Point(this.xc-Math.sqrt(Math.abs(2*(rc- this.yc)/3*this.kc)) ,this.yc)
        let point14= new Point(this.xc,this.yc)
        let point15= new Point(this.xc+Math.sqrt(Math.abs(2*(rc- this.yc)/3*this.kc)),this.yc)
        let point16= new Point(1-(rc* 1/Math.tan(Math.abs(this.at)/180* Math.PI)),rc)
        let point17= new Point(1,0)
        pickPoints1.push(point11)
        pickPoints1.push(point12)
        pickPoints1.push(point13)
        pickPoints1.push(point14)
        pickPoints1.push(point15)
        pickPoints1.push(point16)
        pickPoints1.push(point17)

        this.crownCADPlugin.command.clearCommand();
        //绘制上翼面草图，命名为sketch1
        this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch1', datumId: 8});
        //创建上翼面翼型曲线
        this.crownCADPlugin.command.sketch.createStyledSpline({
          pickPoints: pickPoints,
          isClosed: 0,
          splineType: 0,
          constraints: [],
        })

        //退出草图
        this.crownCADPlugin.command.sketch.exitSketch();
        //实现草图操作，参数true会覆盖之前的草图，不传则不会
        await this.crownCADPlugin.command.execute(true)
        this.crownCADPlugin.command.clearCommand();
        //绘制下翼面草图，命名为sketch2
        this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch2', datumId: 8});
        //创建下翼面翼型曲线
        this.crownCADPlugin.command.sketch.createStyledSpline({
          pickPoints: pickPoints1,
          isClosed: 0,
          splineType: 0,
          constraints: [],
        })


        //创建镜像线，x轴方向的，镜像线是为了将下翼面翼型曲线镜像到x轴下方
        this.crownCADPlugin.command.sketch.createReferenceLine({
          startPoint: new Point(0,0,0),
          endPoint:new Point(10,0,0)
        })
        //退出草图
        this.crownCADPlugin.command.sketch.exitSketch();

        await this.crownCADPlugin.command.execute()
        //草图2中找到镜像线和放样曲线
        let res = await this.crownCADPlugin.query.getElementsByFeatureNames(["sketch2"]);
        let sketchEls= []
        let lineId
        let axisId
        if(res.code=== 200) sketchEls= res.data.elements
        for (let i = 0; i < sketchEls.length; i++) {
          if (sketchEls[i].params.type=== 16) {
            // 下翼面翼型曲线id
            lineId= sketchEls[i].id
          } else if (sketchEls[i].params.type=== 10) {
            // 镜像线id
            axisId = sketchEls[i].id;
          }
        }
        let basePnt = new Point(0, 0, 0);
        let endPnt = new Point(10, 0, 0);
        let axis = new Axis();

        //镜像曲线

        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch2'});
        // 创建下翼面翼型曲线镜像线
        this.crownCADPlugin.command.sketch.createMirror({
          elementIds: [lineId],
          axisId: axisId,
        })

        // 删除镜像线
        this.crownCADPlugin.command.sketch.deleteElements({
          elementIds: [lineId],
        })
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch2'});
        await this.crownCADPlugin.command.execute()
      },

      //后台求实根
     async solveQuartic(nums) {
       let url = '/bk/calculate';
       let config = {
         timeout: 15000,
         headers: {
           'Content-Type': 'application/json'
         }
       };
       let res = await request.post(url,JSON.stringify(nums),config);
       if(res.status=== 200) {
         return res.data
       }
      }
    }
  }
</script>

<style scoped>
  >>> .el-form-item {
    margin-bottom: 5px!important;
  }
  >>> .el-form-item__label {
    margin-bottom: 5px!important;
  }

  >>> .el-form-item__content {
    display: flex;
    align-items: center;
  }

  >>> .el-form-item {
    height: 32px!important;
  }
  >>> .el-form-item__label {
    line-height: 32px !important;
    width: 120px!important;
  }

</style>
